package com.huawei.hmsdemo10086.Fragment;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huawei.hmsdemo10086.Activity.AddressActivity;
import com.huawei.hmsdemo10086.Activity.Apply_For_VIPActivity;
import com.huawei.hmsdemo10086.Activity.CarActivity;
import com.huawei.hmsdemo10086.Activity.GoodsActivity;
import com.huawei.hmsdemo10086.Activity.LoginActivity;
import com.huawei.hmsdemo10086.Activity.OrderActivity;
import com.huawei.hmsdemo10086.Activity.ShopActivity;
import com.huawei.hmsdemo10086.Activity.Update_PersonActivity;
import com.huawei.hmsdemo10086.POJO.Goods;
import com.huawei.hmsdemo10086.POJO.Result;
import com.huawei.hmsdemo10086.POJO.User;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MyFragment extends Fragment {

    View view;
    private SharedPreferences sharedPreferences;
    private String userId;
    private String userName;
    private boolean isVIP;
    private TextView point;
    private Result<Float> point_res = null;
    private String url = "https://img0.baidu.com/it/u=3311900507,1448170316&fm=26&fmt=auto&gp=0.jpg";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_my, container, false);
        sharedPreferences = getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userName = sharedPreferences.getString("userName",null);

        userId = sharedPreferences.getString("userId",null);
        isVIP = sharedPreferences.getBoolean("isVIP",false);
        Log.d("userId", userId);

        TextView my_name_text = view.findViewById(R.id.my_name_text);
        my_name_text.setText(userName);
        Button my_order_manger = view.findViewById(R.id.my_order_manger);
        Button my_address_manger = view.findViewById(R.id.my_address_manger);
        Button wach_goods = view.findViewById(R.id.watch_goods);
        Button to_be_vip = view.findViewById(R.id.to_be_vip);
        Button add_money = view.findViewById(R.id.add_money);
        Button car = view.findViewById(R.id.buy_car);
        Button logout = view.findViewById(R.id.logout);
        ImageView head_img = view.findViewById(R.id.my_img);
        point = view.findViewById(R.id.point_text);

        point.setText("积分:"+sharedPreferences.getFloat("point",0));
        System.out.println(isVIP);


        Glide.with(getActivity()).load(url)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(head_img);

        if(isVIP)
        {
            my_order_manger.setVisibility(View.VISIBLE);
            my_address_manger.setVisibility(View.VISIBLE);
            wach_goods.setVisibility(View.VISIBLE);
            to_be_vip.setVisibility(View.GONE);
            add_money.setVisibility(View.VISIBLE);
            ImageView vip_icon = view.findViewById(R.id.vip_icon);
            vip_icon.setVisibility(View.VISIBLE);
            car.setVisibility(View.VISIBLE);
        }
        else
        {
            my_order_manger.setVisibility(View.GONE);
            my_address_manger.setVisibility(View.GONE);
            wach_goods.setVisibility(View.GONE);
            to_be_vip.setVisibility(View.VISIBLE);
            add_money.setVisibility(View.GONE);
            car.setVisibility(View.GONE);

        }

        car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CarActivity.class);
                startActivity(intent);
            }
        });

        add_money.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add_money();
                Toast.makeText(getActivity(), "成功", Toast.LENGTH_SHORT).show();
            }
        });

        head_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Update_PersonActivity.class);
                startActivity(intent);
            }
        });

        my_address_manger.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddressActivity.class);
                startActivity(intent);
            }
        });

        wach_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ShopActivity.class);
                startActivity(intent);
            }
        });

        my_order_manger.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), OrderActivity.class);
                startActivity(intent);
            }
        });

        to_be_vip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Apply_For_VIPActivity.class);
                startActivity(intent);
            }
        });

        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences preferences = getContext().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
                editor.clear();
                editor.commit();

                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });


        return view;
    }

    public void add_money()
    {
        String url = "http://10.0.2.2:8443/api/addMoney";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"userid\": \"" + userId + "\"\n" +
                "}";

        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                String rep = response.body().string();
                Log.d("point", rep);
                Type type = new TypeToken<Result<Float>>(){}.getType();
                point_res = gson.fromJson(rep,type);
                point.setText("积分:"+point_res.getData());

                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putFloat("point",(float) point_res.getData());
                editor.commit();

            }
        });
    }
}